//
//  BIDPublishRentInfoViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/14.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDPublishRentInfoViewController.h"
#import "BIDRentConditionListViewController.h"
#import "BIDBrowserImageViewController.h"
#import "UIView+FindFirstResponder.h"
#import "ELCImagePickerHeader.h"
#import "BIDAddPhotoView.h"
#import "BIDUploadFile.h"
#import "BIDCustomTextView.h"

#import <MobileCoreServices/UTCoreTypes.h>
#import "BIDPublishRentFooterView.h"
#import "BIDRentForPublishCell.h"

//房屋类型、支付方式等的获取
static NSString *rentConditionURL = @"commonAndroid/getDictsByCode.action";

//发布出租信息
static NSString *publishURL = @"rentAndroid/add.action";

//修改
static NSString *editURL = @"rentAndroid/modify.action";

@interface BIDPublishRentInfoViewController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, BIDRentConditionListViewControllerDelegate, BIDPublishRentFooterViewDelegate, ELCImagePickerControllerDelegate, UINavigationControllerDelegate, BIDUploadFileDelegate, UIScrollViewDelegate>
{
    NSMutableArray *_dataSourceArr;
    NSArray *_houseTypeArr;
    NSArray *_payTypeArr;
    NSArray *_rentTypeArr;
    //
    NSUInteger _maxImgCount;
    //
    BIDPublishRentFooterView *_footerView;
    BIDUploadFile *_uploadFile;
    //获取焦点的编辑框的位置
    CGRect _desRect;
    BOOL _isRegister;
}
@end

@implementation BIDPublishRentInfoViewController
@synthesize isEdit;
@synthesize rentInfoDic;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"出租";
    _maxImgCount = 3;
    // Do any additional setup after loading the view from its nib.
    _tableView.tableFooterView = [[UIView alloc] init];
    if(isEdit)
    {
    }
    else
    {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(closeHandler)];
    }
    //
    NSString *strRequestURL = @"";
    if(isEdit)
    {
        strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], editURL];
    }
    else
    {
        strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], publishURL];
    }
    _uploadFile = [[BIDUploadFile alloc] initWithURL:strRequestURL];
    _uploadFile.delegate = self;
    //
    _footerView = [[[NSBundle mainBundle] loadNibNamed:@"BIDPublishRentFooterView" owner:self options:nil] lastObject];
    _footerView.delegate = self;
    _footerView.contentTextView.placeholder = @"请输入需要补充的内容";
    _footerView.contentTextView.inputAccessoryView = self.toolBar;
    _tableView.tableFooterView = _footerView;
    //
    [self prepareData];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareData
{
    _houseTypeArr = [[NSArray alloc] init];
    _payTypeArr = [[NSArray alloc] init];
    _rentTypeArr = [[NSArray alloc] init];
    NSArray *tempArr = nil;
    if(isEdit)
    {
        tempArr = @[@{@"title":@"联系人", @"content":rentInfoDic[@"contactName"], @"value":rentInfoDic[@"contactName"], @"placeholder":@"请输入您的姓名", @"keyboardType":@"normal", @"menuArr":[NSNull null], @"field":@"rent.contactName"},
                    @{@"title":@"手机号码", @"content":rentInfoDic[@"tel"], @"value":rentInfoDic[@"tel"], @"placeholder":@"请输入您的手机号码", @"keyboardType":@"phone", @"menuArr":[NSNull null], @"field":@"rent.tel"},
                    @{@"title":@"地址", @"content":rentInfoDic[@"address"], @"value":rentInfoDic[@"address"], @"placeholder":@"请输入房屋地址", @"keyboardType":@"normal", @"menuArr":[NSNull null], @"field":@"rent.address"},
                    @{@"title":@"户型", @"content":rentInfoDic[@"houseTypeName"], @"value":rentInfoDic[@"houseType"], @"placeholder":@"请选择户型", @"keyboardType":@"normal", @"menuArr":_houseTypeArr, @"field":@"rent.houseType"},
                    @{@"title":@"楼层", @"content":rentInfoDic[@"floor"], @"value":rentInfoDic[@"floor"], @"placeholder":@"请输入楼层", @"keyboardType":@"number", @"menuArr":[NSNull null], @"field":@"rent.floor"},
                    @{@"title":@"总楼层", @"content":rentInfoDic[@"totalFloors"], @"value":rentInfoDic[@"totalFloors"], @"placeholder":@"请输入总楼层", @"keyboardType":@"number", @"menuArr":[NSNull null], @"field":@"rent.totalFloors"},
                    @{@"title":@"面积/㎡", @"content":rentInfoDic[@"area"], @"value":rentInfoDic[@"area"], @"placeholder":@"请输入面积", @"keyboardType":@"decimal", @"menuArr":[NSNull null], @"field":@"rent.area"},
                    @{@"title":@"租金(元/月)", @"content":rentInfoDic[@"rental"], @"value":rentInfoDic[@"rental"], @"placeholder":@"请输入租金", @"keyboardType":@"decimal", @"menuArr":[NSNull null], @"field":@"rent.rental"},
                    @{@"title":@"付款方式", @"content":rentInfoDic[@"payModeName"], @"value":rentInfoDic[@"payMode"], @"placeholder":@"请选择付款方式", @"keyboardType":@"normal", @"menuArr":_payTypeArr, @"field":@"rent.payMode"},
                    @{@"title":@"出租方式", @"content":rentInfoDic[@"rentModeName"], @"value":rentInfoDic[@"rentMode"], @"placeholder":@"请选择出租方式", @"keyboardType":@"normal", @"menuArr":_rentTypeArr, @"field":@"rent.rentMode"}];
        //
        _footerView.contentTextView.text = rentInfoDic[@"content"];
        NSArray *arr = rentInfoDic[@"fileList"];
        if(arr && arr.count>0)
        {
            for(NSDictionary *dic in arr)
            {
                NSString *strFilePath = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], dic[@"path"]];
                [_footerView.addPhotoView addPhotoWithImgURL:[NSURL URLWithString:strFilePath]];
            }
        }
    }
    else
    {
        tempArr = @[@{@"title":@"联系人", @"content":@"", @"value":@"", @"placeholder":@"请输入您的姓名", @"keyboardType":@"normal", @"menuArr":[NSNull null], @"field":@"rent.contactName"},
                    @{@"title":@"手机号码", @"content":@"", @"value":@"", @"placeholder":@"请输入您的手机号码", @"keyboardType":@"phone", @"menuArr":[NSNull null], @"field":@"rent.tel"},
                    @{@"title":@"地址", @"content":@"", @"value":@"", @"placeholder":@"请输入房屋地址", @"keyboardType":@"normal", @"menuArr":[NSNull null], @"field":@"rent.address"},
                    @{@"title":@"户型", @"content":@"", @"value":@"", @"placeholder":@"请选择户型", @"keyboardType":@"normal", @"menuArr":_houseTypeArr, @"field":@"rent.houseType"},
                    @{@"title":@"楼层", @"content":@"", @"value":@"", @"placeholder":@"请输入楼层", @"keyboardType":@"number", @"menuArr":[NSNull null], @"field":@"rent.floor"},
                    @{@"title":@"总楼层", @"content":@"", @"value":@"", @"placeholder":@"请输入总楼层", @"keyboardType":@"number", @"menuArr":[NSNull null], @"field":@"rent.totalFloors"},
                    @{@"title":@"面积/㎡", @"content":@"", @"value":@"", @"placeholder":@"请输入面积", @"keyboardType":@"decimal", @"menuArr":[NSNull null], @"field":@"rent.area"},
                    @{@"title":@"租金(元/月)", @"content":@"", @"value":@"", @"placeholder":@"请输入租金", @"keyboardType":@"decimal", @"menuArr":[NSNull null], @"field":@"rent.rental"},
                    @{@"title":@"付款方式", @"content":@"", @"value":@"", @"placeholder":@"请选择付款方式", @"keyboardType":@"normal", @"menuArr":_payTypeArr, @"field":@"rent.payMode"},
                    @{@"title":@"出租方式", @"content":@"", @"value":@"", @"placeholder":@"请选择出租方式", @"keyboardType":@"normal", @"menuArr":_rentTypeArr, @"field":@"rent.rentMode"}];
    }
    _dataSourceArr = [[NSMutableArray alloc] initWithArray:tempArr];
}

- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], rentConditionURL];
    dispatch_group_t group = dispatch_group_create();
    [self showSpinnerView];
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
        NSString *strPost = [[NSString alloc] initWithFormat:@"searchParam.code=%@", @"HOUSE_TYPE"];
        id obj = [BIDDataCommunication getDataFromNetBySyn:strRequestURL postValue:strPost];
        if([obj isKindOfClass:[NSDictionary class]])
        {
            NSDictionary *dic = (NSDictionary *)obj;
            NSNumber *resValue = dic[@"head"][@"success"];
            if([resValue intValue] == 1)
            {
                NSArray *arr = dic[@"body"];
                _houseTypeArr = [[NSArray alloc] initWithArray:arr];
                NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:_dataSourceArr[3]];
                [tempDic setObject:_houseTypeArr forKey:@"menuArr"];
                [_dataSourceArr replaceObjectAtIndex:3 withObject:tempDic];
            }
        }
    });
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
        NSString *strPost = [[NSString alloc] initWithFormat:@"searchParam.code=%@", @"PAY_MODE"];
        id obj = [BIDDataCommunication getDataFromNetBySyn:strRequestURL postValue:strPost];
        if([obj isKindOfClass:[NSDictionary class]])
        {
            NSDictionary *dic = (NSDictionary *)obj;
            NSNumber *resValue = dic[@"head"][@"success"];
            if([resValue intValue] == 1)
            {
                NSArray *arr = dic[@"body"];
                _payTypeArr = [[NSArray alloc] initWithArray:arr];
                NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:_dataSourceArr[8]];
                [tempDic setObject:_payTypeArr forKey:@"menuArr"];
                [_dataSourceArr replaceObjectAtIndex:8 withObject:tempDic];
            }
        }
    });
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
        NSString *strPost = [[NSString alloc] initWithFormat:@"searchParam.code=%@", @"RENT_MODE"];
        id obj = [BIDDataCommunication getDataFromNetBySyn:strRequestURL postValue:strPost];
        if([obj isKindOfClass:[NSDictionary class]])
        {
            NSDictionary *dic = (NSDictionary *)obj;
            NSNumber *resValue = dic[@"head"][@"success"];
            if([resValue intValue] == 1)
            {
                NSArray *arr = dic[@"body"];
                _rentTypeArr = [[NSArray alloc] initWithArray:arr];
                NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithDictionary:_dataSourceArr[9]];
                [tempDic setObject:_rentTypeArr forKey:@"menuArr"];
                [_dataSourceArr replaceObjectAtIndex:9 withObject:tempDic];
            }
        }
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [self hideSpinnerView];
    });
}

- (void)closeHandler
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}

- (IBAction)publishBtnHandler
{
    for(NSDictionary *dic in _dataSourceArr)
    {
        NSString *strContent = ANY2NSSTRING(dic[@"content"]);
        if(strContent.length == 0)
        {
            NSString *strMsg = [[NSString alloc] initWithFormat:@"%@不能为空", dic[@"title"]];
            [self showAlertView:strMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
            return;
        }
    }
    if(_footerView.contentTextView.text.length == 0)
    {
        [self showAlertView:@"请填写描述内容" buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        return;
    }
    //
    if(_footerView.addPhotoView.imgArr.count>0)
    {
        if(isEdit)
        {
            [_uploadFile addData:@"rent.id" value:rentInfoDic[@"id"]];
            [_uploadFile addData:@"rent.states" value:rentInfoDic[@"states"]];
            [_uploadFile addData:@"rent.rentVerify.id" value:rentInfoDic[@"rentVerify"][@"rentId"]];
        }
        [_uploadFile addData:@"memberId" value:[AppDelegate getUserId]];
        for(NSDictionary *dic in _dataSourceArr)
        {
            [_uploadFile addData:dic[@"field"] value:dic[@"value"]];
        }
        int count = 0;
        for(UIImage *img in _footerView.addPhotoView.imgArr)
        {
            NSData *imgData = UIImageJPEGRepresentation(img, 0.5f);
            NSString *strFileName = [[NSString alloc] initWithFormat:@"fileName%d.jpg", count];
            [_uploadFile addFileFromMemory:strFileName fieldName:@"fileUploads" data:imgData];
            [_uploadFile addData:@"fileUploadsFileName" value:strFileName];
        }
        [_uploadFile addData:@"rent.content" value:_footerView.contentTextView.text];
        [_uploadFile finishAddFile];
        [_uploadFile startUpload];
    }
    else
    {
        NSString *strRequestURL = @"";
        if(isEdit)
        {
            strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], editURL];
        }
        else
        {
            strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], publishURL];
        }
        NSMutableString *strPost = [NSMutableString string];
        [strPost appendFormat:@"memberId=%@", [AppDelegate getUserId]];
        for(NSDictionary *dic in _dataSourceArr)
        {
            [strPost appendFormat:@"&%@=%@", dic[@"field"], dic[@"value"]];
        }
        [strPost appendFormat:@"&rent.content=%@", _footerView.contentTextView.text];
        if(isEdit)
        {
            [strPost appendFormat:@"&rent.id=%@", rentInfoDic[@"id"]];
            [strPost appendFormat:@"&rent.status=%@", rentInfoDic[@"states"]];
            [strPost appendFormat:@"&rent.rentVerify.id=%@", rentInfoDic[@"rentVerify"][@"rentId"]];
        }
        [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
            if(errMsg)
            {
                [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
            }
            else
            {
                if(isEdit)
                {
                    [self showAlertView:@"发布成功" buttonTitle:ALERT_BTN_TITLE tag:@101 functionName:@"alertViewHandler:"];
                }
                else
                {
                    [self showAlertView:@"发布成功" buttonTitle:ALERT_BTN_TITLE tag:@100 functionName:@"alertViewHandler:"];
                }
            }
        }];
    }
}

- (void)alertViewHandler:(id)obj
{
    NSNumber *value = (NSNumber *)obj;
    if([value integerValue] == 101)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:^{}];
    }
}

/**
 键盘显示与隐藏

 @param notification <#notification description#>
 */
- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    self.keyboardHeight = keyboardSize.height;
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.25f];
    UIView *tempView = [self.view findFirstResponder];
    if([tempView isKindOfClass:[UITextField class]])
    {
        if(_desRect.origin.y+_desRect.size.height > CGRectGetHeight(self.view.frame)-self.keyboardHeight)
        {
            self.distanceToMove = _desRect.origin.y+_desRect.size.height-(CGRectGetHeight(self.view.frame)-self.keyboardHeight);
            CGPoint pt = _tableView.contentOffset;
            pt.y += self.distanceToMove;
            [_tableView setContentOffset:pt animated:YES];
        }
    }
    else if([tempView isKindOfClass:[UITextView class]])
    {
        CGFloat textViewY = CGRectGetMinY(_footerView.frame) + 10;
        if(textViewY+tempView.frame.size.height-_tableView.contentOffset.y > CGRectGetHeight(self.view.frame)-self.keyboardHeight)
        {
            self.distanceToMove = textViewY+tempView.frame.size.height-_tableView.contentOffset.y - CGRectGetHeight(self.view.frame)+self.keyboardHeight;
            CGPoint pt = _tableView.contentOffset;
            pt.y += self.distanceToMove;
            [_tableView setContentOffset:pt animated:YES];
        }
    }
    
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.30f];
    CGPoint pt = _tableView.contentOffset;
    if(pt.y-self.distanceToMove < 0)
    {
        pt.y = 0;
    }
    else
    {
        pt.y -= self.distanceToMove;
    }
    [_tableView setContentOffset:pt animated:YES];
    [UIView commitAnimations];
}

#pragma mark - BIDPublishRentFooterViewDelegate
- (void)choosePhoto
{
    if(_footerView.addPhotoView.imgArr.count == _maxImgCount)
    {
        NSString *strHint = [[NSString alloc] initWithFormat:@"最多选择%ld张图片", _maxImgCount];
        [self showAlertView:strHint buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        return;
    }
    //
    ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initImagePicker];
    
    //3-_addPhotoView.imgArr.count
    elcPicker.maximumImagesCount = 111; //Set the maximum number of images to select to 100
    elcPicker.returnsOriginalImage = YES; //Only return the fullScreenImage, not the fullResolutionImage
    elcPicker.returnsImage = YES; //Return UIimage if YES. If NO, only return asset location information
    elcPicker.onOrder = YES; //For multiple image selection, display and return order of selected images
    elcPicker.mediaTypes = @[(NSString *)kUTTypeImage]; //Supports image and movie types((NSString *)kUTTypeMovie)
    
    elcPicker.imagePickerDelegate = self;
    
    [self presentViewController:elcPicker animated:YES completion:nil];
}

- (void)resizeViewWithFrame:(CGRect)frame
{
    _footerView.frame = frame;
    _tableView.tableFooterView = _footerView;
}

- (void)showPhotoAtIndex:(NSInteger)index withImgArr:(NSArray *)arr
{
    BIDBrowserImageViewController *vc = [[BIDBrowserImageViewController alloc] initWithNibName:@"BIDBrowserImageViewController" bundle:nil];
    vc.index = index;
    vc.imgArr = [[NSArray alloc] initWithArray:arr];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - BIDRentConditionListViewControllerDelegate
- (void)getConditionText:(NSString *)content itemID:(NSString *)itemID atIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:_dataSourceArr[row]];
    [dic setObject:content forKey:@"content"];
    [dic setObject:itemID forKey:@"value"];
    [_dataSourceArr replaceObjectAtIndex:row withObject:dic];
    [_tableView reloadData];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
    CGRect rectInTableView = [_tableView rectForRowAtIndexPath:indexPath];
    CGRect rect = [_tableView convertRect:rectInTableView toView:[_tableView superview]];
    _desRect = rect;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSUInteger row = textField.tag;
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:_dataSourceArr[row]];
    [dic setObject:textField.text forKey:@"value"];
    [dic setObject:textField.text forKey:@"content"];
    [_dataSourceArr replaceObjectAtIndex:row withObject:dic];
}

#pragma mark - BIDUpladFileDelegate
- (void)finishUploadWithResult:(id)obj
{
    if([obj isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *dic = (NSDictionary *)obj;
        NSNumber *value = dic[@"head"][@"success"];
        if([value intValue] == 1)
        {
            if(isEdit)
            {
                [self showAlertView:@"发布成功" buttonTitle:ALERT_BTN_TITLE tag:@101 functionName:@"alertViewHandler:"];
            }
            else
            {
                [self showAlertView:@"发布成功" buttonTitle:ALERT_BTN_TITLE tag:@100 functionName:@"alertViewHandler:"];
            }
        }
        else
        {
            NSString *strErrMsg = @"";
            if(dic[@"head"][@"msg"])
            {
                strErrMsg = dic[@"head"][@"msg"];
            }
            else
            {
                strErrMsg = @"发布失败";
            }
            [self showAlertView:strErrMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
    }
}

- (void)uploadFailedWithError:(NSError *)err
{
    [self showAlertView:[err localizedDescription] buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
}

#pragma mark - ELCImagePickerControllerDelegate Methods

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
    for (NSDictionary *dict in info)
    {
        if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto)
        {
            if ([dict objectForKey:UIImagePickerControllerOriginalImage])
            {
                UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];
                [images addObject:image];
                [_footerView.addPhotoView addPhoto:image];
            }
            else
            {
                NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
            }
        }
        else if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypeVideo)
        {
            if ([dict objectForKey:UIImagePickerControllerOriginalImage])
            {
                UIImage* image=[dict objectForKey:UIImagePickerControllerOriginalImage];
                [images addObject:image];
            }
            else
            {
                NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
            }
        }
        else
        {
            NSLog(@"Uknown asset type");
        }
    }
    //_chosenImgArr = images;
}

- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_isRegister)
    {
        _isRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDRentForPublishCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDRentForPublishCell *rentCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = _dataSourceArr[row];
    rentCell.titleLabel.text = dic[@"title"];
    rentCell.contentTF.text = ANY2NSSTRING(dic[@"content"]);
    rentCell.contentTF.placeholder = dic[@"placeholder"];
    rentCell.contentTF.tag = row;
    rentCell.contentTF.delegate = self;
    if([dic[@"keyboardType"] isEqualToString:@"normal"])
    {
        rentCell.contentTF.keyboardType = UIKeyboardTypeDefault;
    }
    else if([dic[@"keyboardType"] isEqualToString:@"phone"])
    {
        rentCell.contentTF.keyboardType = UIKeyboardTypePhonePad;
    }
    else if([dic[@"keyboardType"] isEqualToString:@"number"])
    {
        rentCell.contentTF.keyboardType = UIKeyboardTypeNumberPad;
    }
    else if([dic[@"keyboardType"] isEqualToString:@"decimal"])
    {
        rentCell.contentTF.keyboardType = UIKeyboardTypeDecimalPad;
    }
    if(ISNULL(dic[@"menuArr"]))
    {
        rentCell.contentTF.enabled = YES;
        rentCell.accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        rentCell.contentTF.enabled = NO;
        rentCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    rentCell.contentTF.inputAccessoryView = self.toolBar;
    cell = rentCell;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSUInteger row = indexPath.row;
    NSDictionary *dic = _dataSourceArr[row];
    if(ISNULL(dic[@"menuArr"]))
    {
        return;
    }
    BIDRentConditionListViewController *vc = [[BIDRentConditionListViewController alloc] init];
    vc.dataSourceArr = [[NSArray alloc] initWithArray:dic[@"menuArr"]];
    vc.selectedIndexPath = indexPath;
    vc.selectedItemID = dic[@"value"];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
